'''
Created on Apr 6, 2013

@author: priya
'''
# Project for creating wordclouds of subreddit. 
'''
How is it going to work? 
    - get subreddit name from user 
    - get top 25 posts
        - titles 
        - comments
        - url domains
    - suffix trees or something
    - get the top x phrases 
    - present them as a wordcloud. 
    
Extras: 
    - User summaries 
    - user wordclouds 
    - user domains, subreddits. 
'''
import praw
from collections import defaultdict
import string
import urlparse

class SubredditCloud:
     
    redditObj = None
    numSubmissions = 100
    numComments = 5
    stopWordList = []
    stopwordFile = './stopwords.txt'
    defaultUserAgent = ("Subreddit cloud"
              "priya-ven.appspot.com")
    topLevelDomains = {'.com'}
    
    wordFreqList = defaultdict(int)
    
    def __init__(self,ipstopWdString):
        self.redditObj = praw.Reddit(user_agent=self.defaultUserAgent)
        #stopwdFileHandle = open(self.stopwordFile)
        stopWdString = ipstopWdString #stopwdFileHandle.read()
        for c in string.punctuation:
            stopWdString = stopWdString.replace(c,"")
        stopWdString = stopWdString.replace('\r',"")
        self.stopWordList = stopWdString.split('\n')
        #stopwdFileHandle.close()
        
    def addToWordlist(self,wordstring):
        if self.stopWordList is None or len(self.stopWordList) <2:
            raise Exception("StopWordsMissing")  
        wordstring = wordstring.lower()
        for c in string.punctuation:
            wordstring = wordstring.replace(c,"")
        words = wordstring.split()
        for word in words: 
            if word not in self.stopWordList:
                self.wordFreqList[word] = self.wordFreqList[word] + 1
        
    def subredditSummary(self,subreddit):
        # get submissions 
        submission = None
        try:
            topSubmissions = self.redditObj.get_subreddit(subreddit).get_hot(limit = self.numSubmissions)
            submission = next(topSubmissions)
        except:
            print 'Can\'t access this subreddit for whatever reason'
            return
            
        while(submission is not None): 
            try:
                #print submission.title
                self.addToWordlist(submission.title)
                url = urlparse.urlparse(submission.url).hostname.split('.')
                url = ".".join(len(url[-2]) < 4 and url[-3:-2] or url[-2:-1])
                self.addToWordlist(url)
                submission = next(topSubmissions)
            except StopIteration:
                break
            except: 
                return 'Can\'t access this subreddit for whatever reason'
                
        sortedList = sorted(self.wordFreqList.items(), key=lambda x: x[1])
        sortedList.reverse()
        return sortedList[:100]
            
'''
subredditcloud = SubredditCloud()
wordList = subredditcloud.subredditSummary('AdviceAnimals')
cloudstring = ""
for tup in wordList:
    cloudstring = cloudstring + ',' +','.join(['"' + tup[0] + '"']*tup[1])
print cloudstring[1:]
'''


    


    